package com.tkc.common.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import com.tkc.common.enums.BaseResultCodeEnum;
import com.tkc.common.exception.ResponseException;

/**
 * 
 * @Function: 用于分页处理
 * 
 * @Project :assessmenttask 
 * @Package :com.tkc.common.util 
 * @File :Paging.java 
 * @ClassName: Paging  
 * 
 * @author tankaichao 
 * @email cgztkc@126.com 
 * @Date 2016年8月11日上午10:50:38
 * @since JDK 1.8 
 *
 */
public class Paging {
	public static Sort getPageSort(String orderBy){
		try{
			if(orderBy == null || orderBy.equals("")){
				return null;
			}
			String[] orderStrs = orderBy.split(";");
			List<Order> orderList = new ArrayList<Order>();
			for(String orderStr : orderStrs){
				String direction = orderStr.split(",")[1].toUpperCase();
				String item = orderStr.split(",")[0];
				Order order = new Order(Direction.valueOf(direction),item);
				orderList.add(order);
			}
			Sort sort = new Sort(orderList);
			return sort;
		}catch(Exception ex){
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20009);
		}
	}
}
